import 'package:flutter/material.dart';
import 'package:flutter_blog_manage_study/base/get/get_extension.dart';
import 'package:flutter_blog_manage_study/res/style.dart';
import 'package:flutter_blog_manage_study/widget/over_scroll_behavior.dart';
import 'package:get/get.dart';

/// * 作者: Tao
/// * 日期: 2024/3/20
/// * 时间: 11:00
/// * 描述: 公共弹窗
class CommonDialog extends StatelessWidget {
  /// 标题
  final String title;

  /// 内容
  final String content;

  /// 确定按钮文本
  final String nextText;

  /// 取消按钮文本
  final String backText;

  /// 确定按钮事件
  final VoidCallback? nextTap;

  /// 取消按钮事件
  final VoidCallback? backTap;

  /// 是否显示确定按钮
  final bool nextVisible;

  /// 是否显示取消按钮
  final bool backVisible;

  const CommonDialog({
    super.key,
    this.title = '',
    this.content = '',
    this.backText = '',
    this.nextText = '',
    this.backVisible = true,
    this.nextVisible = true,
    this.backTap,
    this.nextTap,
  });

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency, //透明类型
      child: Center(
        // 居中
        child: ScrollConfiguration(
            // 取消ListView滑动阴影
            behavior: OverScrollBehavior(),
            child: ListView(
              shrinkWrap: true, // ListView 的shrinkWrap属性可适应高度（有多少占多少）
              children: [
                Container(
                    width: double.infinity,
                    margin: const EdgeInsets.symmetric(horizontal: 24),
                    child: Container(
                      decoration: const ShapeDecoration(
                        color: Colors.white,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(8.0)),
                        ),
                      ),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Box.vBox15,
                          Text(title, style: TextStyles.style_black_18_bold500),
                          Padding(
                              //加边距
                              padding: const EdgeInsets.symmetric(
                                  vertical: 12, horizontal: 16),
                              child: Text(content,
                                  style: TextStyles.style_6A6969_16)),
                          DividerStyle.divider1Half,
                          Flex(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            direction: Axis.horizontal,
                            children: [
                              _buildTextButton(backTap, backText, backVisible),
                              _buildTextButton(nextTap, nextText, nextVisible),
                            ],
                          )
                        ],
                      ),
                    ))
              ],
            )),
      ),
    );
  }

  /// 构建按钮
  /// * [onTap] 点击事件
  /// * [text] 按钮文本
  /// * [show] 是否显示
  _buildTextButton(VoidCallback? onTap, String text, bool show) {
    return Visibility(
      visible: show,
      child: Expanded(
          flex: 1,
          child: TextButton(
            onPressed: () {
              Get.dismiss();
              onTap?.call();
            },
            style: ButtonStyles.getNoShapeStyle(),
            child: Container(
              alignment: Alignment.center,
              height: 60,
              child: Text(text, style: TextStyles.style_black_16),
            ),
          )),
    );
  }
}
